$(document).ready(function () {

    $('#btnAdd').bind('click', btnAddClick)
    $('#btnEdit').bind('click',btnEditClick)
    $('#btnSave').bind('click', btnSaveClick)
    $('#btnRemove').bind('click',btnRemoveClick)

    initTable();


})


function initTable() {

    $('#list').bootstrapTable({
        /*  data: [
              {username: 'amdin', password: '123'},
              {username: 'amdin', password: '123'}
          ],*/
        url: 'http://localhost:8080/java_web_db_war_exploded/room/list',
        classes: "table table-bordered table-striped",
        singleSelect: true,
        clickToSelect: true,
        search: true,
        pagination: true,
        pageSize: 5,
        pageNumber: 1, //当前地基页
        toolbar: '#toolbar',
        method: 'get',
        dataType: 'json',
        columns: columns
    })
}

var columns = [
    {
        checkbox: true
    },
    {
        field: 'roomNO',
        title: '用户名'
    }, {
        field: 'price',
        title: '密码'
    }
]


function btnAddClick() {
    $('#info').modal();
}

function btnSaveClick() {
    $.ajax({
        url: 'http://localhost:8080/java_web_db_war_exploded/user/add',
        method: 'post',
        dataType: 'json',
        data: {
             userId:$('#user_id').val(),
            username: $('#username').val(),
            password: $('#password').val()
        }, success: function (data) {
            if (data.flag) {
                $('#info').modal('hide');
            }

        }

    })
}

function btnEditClick(){
    var row = $('#list').bootstrapTable('getSelections')[0];

    $('#user_id').val(row.roomId)
    $('#username').val(row.roomNO);
    $('#password').val(row.price);

    $('#info').modal();
}


function btnRemoveClick(){
 /*   var code= prompt("请输入你的验证码");
    alert(code);*/

    if(window.confirm("是否删除当前数据？")){ // BOM  alert  confirm prompt
        var row = $('#list').bootstrapTable('getSelections')[0];
        $.ajax({
            url:'',
            method:'post',
            dataType:'json',
            data:{
                userId:row.userId
            },success:function (data) {
                if(data.flag){
                    alert("删除成功")
                }else{
                    alert("删除失败")

                }
            }
        })
    }
}